package com.spgf.model.dao;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.model.dao.util.PadraoDAOAb;
import com.spgf.model.entities.Categoria;
import com.spgf.model.entities.Lancamento;
import com.spgf.model.enums.FormaPagamento;
import com.spgf.model.utils.AndroidUtil;

public class LancamentoDAO extends PadraoDAOAb<Lancamento> {

	public final static String	NOME_TABELA				= "TBLANCAMENTO";

	public final static String	COLUNA_CODIGO			= "CD_LANCAMENTO";
	public final static String	COLUNA_OBSERVACAO		= "DS_OBSERVACAO";
	public final static String	COLUNA_CATEGORIA		= "CD_CATEGORIA";
	public final static String	COLUNA_VALOR			= "VL_LANCAMENTO";
	public final static String	COLUNA_DATA				= "DT_LANCAMENTO";
	public final static String	COLUNA_FORMA_PAGAMENTO	= "TP_FORMA_PAGAMENTO";

	public LancamentoDAO(final Context context) {
		super(context);
	}

	@Override
	public Lancamento configurarDependencias(final Lancamento model) throws NegocioException {
		model.setCategoria(new CategoriaDAO(getContext()).executeFind(new Categoria(model.getCategoria().getCodigo())));
		return model;
	}

	@Override
	protected Lancamento configurarModel(final Cursor cursor) {
		final Lancamento newModel = new Lancamento();
		newModel.setCodigo(cursor.getInt(cursor.getColumnIndex(COLUNA_CODIGO)));
		newModel.setData(AndroidUtil.toDate(cursor.getString(cursor.getColumnIndex(COLUNA_DATA))));
		newModel.setObservacao(cursor.getString(cursor.getColumnIndex(COLUNA_OBSERVACAO)));
		newModel.setValor(cursor.getDouble(cursor.getColumnIndex(COLUNA_VALOR)));
		newModel.setFormaPagamento(FormaPagamento.values()[cursor.getInt(cursor.getColumnIndex(COLUNA_FORMA_PAGAMENTO))]);
		newModel.setCategoria(new Categoria(cursor.getInt(cursor.getColumnIndex(COLUNA_CATEGORIA))));
		return newModel;
	}

	@Override
	protected String configurarOrderBy(String pOrderBy) {

		if (pOrderBy != null) {
			return " " + pOrderBy + " ASC ";
		}

		return " " + COLUNA_DATA + " ASC ";
	}

	@Override
	protected ContentValues configureContetValues(final Lancamento model) {
		final ContentValues values = new ContentValues();
		values.put(COLUNA_DATA, AndroidUtil.toStringSQLite(model.getData()));
		values.put(COLUNA_OBSERVACAO, model.getObservacao());
		values.put(COLUNA_VALOR, model.getValor());
		values.put(COLUNA_FORMA_PAGAMENTO, model.getFormaPagamento().ordinal());
		values.put(COLUNA_CATEGORIA, model.getCategoria().getCodigo());
		return values;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	protected String definirClausuraWhere(final Lancamento model) {

		final Calendar calAtual = Calendar.getInstance();

		if (model.getData() != null) {
			calAtual.setTime(model.getData());
		}

		if (calAtual.get(Calendar.MONTH) != Calendar.getInstance().get(Calendar.MONTH)) {
			calAtual.set(Calendar.DATE, calAtual.getActualMaximum(Calendar.DATE));
		}

		final Calendar calInicial = Calendar.getInstance();
		calInicial.set(Calendar.DATE, 1);
		calInicial.set(Calendar.MONTH, calAtual.get(Calendar.MONTH));

		final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		// SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");

		String sql = " " + COLUNA_DATA + " BETWEEN '" + df.format(calInicial.getTime()) + "' AND '" + df.format(calAtual.getTime()) + "' ";

		if (model.getCategoria() != null && model.getCategoria().getCodigo() != null) {
			sql += " AND " + COLUNA_CATEGORIA + " = " + model.getCategoria().getCodigo();
		}

		return sql;
	}

	@SuppressLint("SimpleDateFormat")
	public StringBuilder gerarQueryStringNative() throws NegocioException {

		final StringBuilder lSql = new StringBuilder();

		final List<Lancamento> models = executeFindAll(null, LancamentoDAO.COLUNA_CODIGO, null);
		for (final Lancamento model : models) {

			// INSERT INTO LANCAMENTO VALUES (102, '2012-05-02 00:00:00', 'OBS', 14.14, 1, 2, 1);

			lSql.append(" INSERT INTO ");
			lSql.append(NOME_TABELA);
			lSql.append(" VALUES ");
			lSql.append("( ");
			lSql.append(model.getCodigo());
			lSql.append(", ");
			lSql.append("'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(model.getData()) + "'");
			lSql.append(", ");
			lSql.append("'" + model.getObservacao() + "'");
			lSql.append(", ");
			lSql.append(model.getValor());
			lSql.append(", ");
			lSql.append(model.getFormaPagamento().getCodigo());
			lSql.append(", ");
			lSql.append(model.getCategoria().getCodigo());
			lSql.append(" ); ");
			// lSql.append("\n");

		}

		return lSql;

	}

	@Override
	public String[] getColuns() {
		return new String[] {COLUNA_CODIGO, COLUNA_CATEGORIA, COLUNA_DATA, COLUNA_FORMA_PAGAMENTO, COLUNA_OBSERVACAO, COLUNA_VALOR};
	}

	@Override
	protected String getNomeTabela() {
		return NOME_TABELA;
	}

	@Override
	protected String getColunaCodigo() {
		return COLUNA_CODIGO;
	}

}
